package com.test.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.Company;
import com.test.domain.User;
import com.test.service.CompanyService;
import com.test.vo.Result;

@Controller
@RequestMapping("company")
public class CompanyController {
@Autowired
private CompanyService companyService;
@RequirePermission("company:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
	return "company/listUI";
}
@RequirePermission("company:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit,String search) {
	
	PageInfo<Company> pageInfo = this.companyService.getListByPage(offset / limit + 1, limit, search);
	return Result.succeed(pageInfo);
}
//===================================保存/修改/删除方法=======================================	
@RequestMapping("saveUI")
public String saveUI(Integer id,HttpServletRequest request) {
	if (id != null) {
		Company company = this.companyService.getById(id);
		if (company != null) {
			request.setAttribute("company", company);
		}
	}
	return "company/saveUI";
}
	
	@RequirePermission("company:add;company:update")
	@RequestMapping("save")
	public String add(Company company) {
		if (company.getId() != null) {
			this.companyService.update(company);
		} else {
			company.setCreateTime(new Date());
			this.companyService.save(company);
		}
		return "redirect:/company/listUI";
	}
	
	@RequirePermission("company:delete")
	@RequestMapping("delete/{ids}")
	@ResponseBody
	public Result delete(@PathVariable("ids") String ids) {
		
		String[] idsStr = ids.split(",");
		if (idsStr.length == 1) {
			this.companyService.deleteById(Integer.parseInt(idsStr[0]));
		} else {
			this.companyService.deleteBatchByIds(idsStr);
		}
		return Result.succeed();
	}

}
